use lavender::db::query_statement::prelude::*;
use lavender::*;

#[test]
fn query_where(){
    let names = vec![5,6,7];
    let filter = lavender_query_filters!({"id" 2},{"type" in_ 15 16},{"cat" not_in 3 4},{"name" in_list names});
    println!("sql:\t\t {} \nreplace:\t\t {:?}",filter.get_sql(),filter.get_bind());

    let filter = lavender_query_filters!({"id" 2},{"age" > 15},{"type in (select id from type_info)"});
    println!("sql:\t\t {} \nreplace:\t\t {:?}",filter.get_sql(),filter.get_bind());
    let other_filter = lavender_query_filters!({"name" "zhunian"},{"score" >= 80});
    println!("sql:\t\t {} \nreplace:\t\t {:?}",other_filter.get_sql(),other_filter.get_bind());

    let filter3 = WhereSqlMaker::link_or(vec![filter,other_filter]);
    println!("sql:\t\t {} \nreplace:\t\t {:?}",filter3.get_sql(),filter3.get_bind());
}

#[test]
fn query_insert(){
    let maker = lavender_query_key_values!({"name" "zhunian"},{"sn" "123456789"},{"created" literal "UNIX_TIMESTAMP()"});
    let res = maker.try_to_insert_maker();
    if res.is_ok() {
        let sql_tpl = res.ok().unwrap();
        println!("sql:\t\t {} \nreplace:\t\t {:?}",sql_tpl.get_sql(),sql_tpl.get_bind());
    }
    else{
        println!("convert failed");
    }
}

#[test]
fn query_update(){
    let maker = lavender_query_key_values!({"name" "zhunian"},{"sn" "123456789"},{"site" 1},{"created" literal "UNIX_TIMESTAMP()"});
    let res = maker.try_to_update_maker();
    if res.is_ok() {
        let sql_tpl = res.ok().unwrap();
        println!("sql:\t\t {} \nreplace:\t\t {:?}",sql_tpl.get_sql(),sql_tpl.get_bind());
    }
    else{
        println!("convert failed");
    }
}

type DeviceBase = (i32,String,i16,i16,i64,i64);

fn get_test_mutil_insert_data() -> Vec<DeviceBase>{
    let mut res = vec![];
    let tn = utils::timestamp();
    for i in 0..5000  {
        res.push((i as i32 ,format!("sn_{}",i*i), (i % 10) as i16,(i / 10 % 10) as i16,tn,tn + (i as i64) + 1));
    }
    res
}

#[test]
fn query_bulk_insert(){
    let mut maker = ColumnSqlMaker::with_keys(vec!["sn","type","site","created","updated"]);
    let mutil_data = get_test_mutil_insert_data();
    maker.push_values_convert(mutil_data, |item|{
        format!("{},'{}',{},{},{},{}",item.0,item.1,item.2,item.3,item.4,item.5).into()
    });
    let res = maker.try_to_bulk_insert_maker();
    if res.is_ok() {
        let sql_tpl = res.ok().unwrap();
        println!("sql:\t\t {} \nreplace:\t\t {:?}",sql_tpl.get_sql(),sql_tpl.get_bind());
    }
    else{
        println!("convert failed");
    }
}